package com.bw.module_service.ui

import android.app.AlertDialog
import android.app.Dialog
import android.location.GnssAntennaInfo.Listener
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels

import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_log.utils.LoggerUtil
import com.bw.module_service.R
import com.bw.module_service.adapter.ParkSpaceAdapter
import com.bw.module_service.databinding.ActivityCarSpaceManagerBinding
import com.bw.module_service.model.ParkSpaceModel
import com.bw.module_service.model.ParkSpaceModelItem
import com.bw.module_service.utils.ToastUtils
import com.bw.module_service.viewmodel.ServiceViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.CarSpaceManagerActivity)
class CarSpaceManagerActivity : BaseActivity<ServiceViewModel,ActivityCarSpaceManagerBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_car_space_manager

    private val mViewModel:ServiceViewModel by viewModels()
    private var mAdapter: ParkSpaceAdapter = ParkSpaceAdapter()
    private var positionAll: Int = 0
    private lateinit var searchList: ParkSpaceModel

    override fun initData() {
        //请求数据
        mViewModel.getParkSpace("标准车位")

        //点击退出
        mBinding.back.setOnClickListener {
            finish()
        }

        mBinding.add.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.AddParkSpaceActivity).navigation()
        }

        //搜索
        mBinding.search.setOnQueryTextListener(object:android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                var list = ParkSpaceModel()
                searchList.forEach {
                    if(it.spaceNumber.contains(query!!)){
                        list.add(it)
                    }else{
                        mAdapter.data.clear()
                        mAdapter.notifyDataSetChanged()
                    }
                }
                mAdapter.data.clear()
                mAdapter.data.addAll(list)
                mAdapter.notifyDataSetChanged()
                return true
            }
            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    mAdapter.setList(searchList)
                    mAdapter.notifyDataSetChanged()
                }
                return true
            }
        })

        // 监听叉号点击事件
        mBinding.search.setOnCloseListener {
            // 清空搜索框并恢复原始数据
            mAdapter.setList(searchList)
            mAdapter.notifyDataSetChanged()
            false // 返回 false 表示允许默认关闭行为（即保留 SearchView 展开状态）
        }
        mAdapter.setOnItemLongClickListener { adapter, view, position ->
            showCustomDeleteDialog( position)
            true
        }

    }

    fun showCustomDeleteDialog(position:Int) {
        positionAll = position
        val dialog = Dialog(this).apply {
            setContentView(R.layout.delete_car)
            window?.setBackgroundDrawableResource(android.R.color.transparent)

            findViewById<Button>(R.id.btn_confirm).setOnClickListener {
                mViewModel.deleteCarSpace(mAdapter.data[position].id!!)
                dismiss()
            }

            findViewById<Button>(R.id.btn_cancel).setOnClickListener {
                dismiss()
            }
        }

        dialog.show()
    }
    override fun initView() {
        mBinding.rv.layoutManager = LinearLayoutManager(this)
        mBinding.rv.adapter = mAdapter
    }

    override fun initListener() {
        mViewModel._parkspaceLiveData.observe(this){
            searchList=it
            mAdapter.setList(it.reversed())
            mAdapter.notifyDataSetChanged()
            LoggerUtil.d("zty","ParkSpaceModel: $it")
        }

        mViewModel._deleteCarSpaceLiveData.observe(this) {
            if (it) {
                ToastUtils.showLong(this, "删除成功")
                mAdapter.removeAt(positionAll)
//                mAdapter.notifyItemRemoved(positionAll)

            } else {
                ToastUtils.showLong(this, "删除成功")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mAdapter.data.clear()
        mViewModel.getParkSpace("标准车位")
    }
}